﻿using UnityEngine;

[DisallowMultipleComponent]
public class TweenPRS : UITweener
{
    public Transform from;
    public Transform to;

    private Transform mTrans;
    private Vector3 mPos;
    private Quaternion mRot;
    private Vector3 mScale;

    private Vector3 mToP;
    private Quaternion mToR;
    private Vector3 mToS;

    /// <summary>
    /// 设置目标
    /// </summary>
    /// <param name="mToP"></param>
    /// <param name="mToR"></param>
    /// <param name="mToS"></param>
    public void SetTo(Vector3 mToP, Quaternion mToR, Vector3 mToS)
    {
        this.mToP = mToP;
        this.mToR = mToR;
        this.mToS = mToS;

        if (mTrans == null)
        {
            mTrans = transform;
        }

        mPos = mTrans.position;
        mRot = mTrans.rotation;
        mScale = mTrans.localScale;
    }

    protected override void OnUpdate(float factor, bool isFinished)
    {
        if (mToP != null)
        {
            float value = 1f - factor;
            if (from != null)
            {
                mTrans.position = from.position * value + mToP * factor;
                mTrans.localScale = from.localScale * value + mToS * factor;
                mTrans.rotation = Quaternion.Slerp(from.rotation, mToR, factor);
            }
            else
            {
                mTrans.position = mPos * value + mToP * factor;
                mTrans.localScale = mScale * value + mToS * factor;
                mTrans.rotation = Quaternion.Slerp(mRot, mToR, factor);
            }
        }
    }
}